package com.weimob.mcloud.wechatwork.customer.export;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.weimob.mcloud.wechatwork.customer.export.sentinel.SentinelCommonBlockHandler;
import com.weimob.mcloud.wechatwork.customer.service.user.base.PaasBaseResponse;
import com.weimob.mcloud.wechatwork.customer.service.user.base.PageResponse;
import com.weimob.mcloud.wechatwork.customer.service.user.exception.BaseErrorCode;
import com.weimob.mcloud.wechatwork.customer.service.user.module.bo.customer.ExternalUserRelationBO;
import com.weimob.mcloud.wechatwork.customer.service.user.module.bo.customer.openapi.CustomerBaseInfoApi;
import com.weimob.mcloud.wechatwork.customer.service.user.module.bo.group.CorpChatGroupListBO;
import com.weimob.mcloud.wechatwork.customer.service.user.module.dto.customer.ExternalUserInfoByWidsDTO;
import com.weimob.mcloud.wechatwork.customer.service.user.module.dto.customer.openapi.CustomerQueryApi;
import com.weimob.mcloud.wechatwork.customer.service.user.module.dto.group.CustomerChatGroupDTO;
import com.weimob.mcloud.wechatwork.customer.service.user.module.dto.group.CustomerGroupPassDTO;
import com.weimob.mcloud.wechatwork.customer.service.user.service.CustomerExport;
import com.weimob.mcloud.wechatwork.customer.service.user.service.CustomerPaasExport;
import com.weimob.mcloud.wechatwork.customer.service.user.service.group.ChatGroupExport;
import com.weimob.mcloud.wechatwork.customer.service.user.utils.PaasResponseUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
 * @Author yu liu  2022/7/23 17:42
 * Description
 * Version 1.0.0
 **/
@Slf4j
@Service
public class CustomerPaasExportImpl implements CustomerPaasExport{


    @Autowired
    private CustomerExport customerExport;

    @Autowired
    private ChatGroupExport chatGroupExport;

    @Value("${customer.group.size:20}")
    private Integer customerGroupSize;

    @Override
    @SentinelResource(value = "getCustomerEmployeeRelationByWids", blockHandlerClass = SentinelCommonBlockHandler.class, blockHandler = "getRelationByWidsBlockHandler")
    public PaasBaseResponse<List<ExternalUserRelationBO>> getCustomerEmployeeRelationByWids(ExternalUserInfoByWidsDTO externalUserInfoByWidsDTO) {
        return PaasResponseUtil.ok(customerExport.getExternalUserRelationByWids(externalUserInfoByWidsDTO));
    }

    @Override
    @SentinelResource(value = "getCustomerGroupsByWids", blockHandlerClass = SentinelCommonBlockHandler.class, blockHandler = "getCustomerGroupsByWidsBlockHandler")
    public PaasBaseResponse<PageResponse<CorpChatGroupListBO>> getCustomerGroupsByWids(CustomerGroupPassDTO requestDTO) {
        if(Objects.isNull(requestDTO)
                || Objects.isNull(requestDTO.getWid())
                ||Objects.isNull(requestDTO.getBasicInfo())
                ||Objects.isNull(requestDTO.getBasicInfo().getBosId())){
            return PaasResponseUtil.error(BaseErrorCode.PARMA_EXCPETION.getCode().toString(),BaseErrorCode.PARMA_EXCPETION.getMsg(),null);
        }
        if(requestDTO==null||requestDTO.getWid()==null){
            return PaasResponseUtil.error(BaseErrorCode.PARMA_EXCPETION.getCode().toString(),BaseErrorCode.PARMA_EXCPETION.getMsg(),null);
        }
        if(requestDTO.getPageSize()==null||requestDTO.getPageNum()==null||requestDTO.getPageNum()>customerGroupSize){
            return PaasResponseUtil.error(BaseErrorCode.SELECT_SIZE_OVER.getCode().toString(),BaseErrorCode.SELECT_SIZE_OVER.getMsg(),null);
        }
        CustomerChatGroupDTO customerChatGroupDTO = new CustomerChatGroupDTO();
        customerChatGroupDTO.setBasicInfo(requestDTO.getBasicInfo());
        customerChatGroupDTO.setPageNum(requestDTO.getPageNum());
        customerChatGroupDTO.setPageSize(requestDTO.getPageSize());
        customerChatGroupDTO.setWid(requestDTO.getWid());
        return PaasResponseUtil.ok(chatGroupExport.getGroupChatListByWid(customerChatGroupDTO));
    }

    /**
     * 查询企微客户基础信息 通过wid或者externalUserId
     *
     * @param queryDTO 入参对象
     * @return SoaResponse<CustomerBaseInfoBO, ?> 返回对象
     */
    @Override
    public PaasBaseResponse<CustomerBaseInfoApi> queryCustomerBaseInfoForApi(CustomerQueryApi queryDTO) {
        return PaasResponseUtil.ok(customerExport.queryCustomerBaseInfoForApi(queryDTO));
    }
}
